package lab03.test12;


import java.sql.*;

public class Test12 {
    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//创建一个桥接器
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC","root","w19971201t");
            //括号里面为数据源名

            statement=connection.createStatement();//创建一个容器


  resultSet=statement.executeQuery("SELECT * FROM t_users ");
            System.out.println("查询所有记录");
  //查询所有记录
            while (resultSet.next()){
//boolean next() throws SQLException;boolean类型是直接可以判断真假，所以不用写是否为空
//next()是用来判断数据库下一行是否为空，第一行为列名
                System.out.print(resultSet.getString(1)+" ");
                //getString为列号
                System.out.print(resultSet.getString(2)+" ");
                System.out.print(resultSet.getString(3)+" ");
                System.out.print(resultSet.getString(4));
            }
            System.out.println();
resultSet=statement.executeQuery("select  * from t_users where usename='admin'");
            System.out.println("查询admin记录");
while (resultSet.next()){
    System.out.print(resultSet.getString(1)+" ");
    //getString为列号
    System.out.print(resultSet.getString(2)+" ");
    System.out.print(resultSet.getString(3)+" ");
    System.out.print(resultSet.getString(4));

}
            System.out.println();
resultSet=statement.executeQuery("select * from t_users where password='123456' and usename='admin'");
            System.out.println("查询用户名为'admin'，密码为'123456'的记录");
            while (resultSet.next()){
                System.out.print(resultSet.getString(1)+" ");
                //getString为列号
                System.out.print(resultSet.getString(2)+" ");
                System.out.print(resultSet.getString(3)+" ");
                System.out.print(resultSet.getString(4));
            }
            System.out.println();
            resultSet=statement.executeQuery("select * from t_users where register_datetime>10000000");
            System.out.println("查询注册时间大于10000000的记录");
            while (resultSet.next()){
                System.out.print(resultSet.getString(1)+" ");
                //getString为列号
                System.out.print(resultSet.getString(2)+" ");
                System.out.print(resultSet.getString(3)+" ");
                System.out.print(resultSet.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


}
